﻿using System;
using System.Globalization;
using System.Windows.Data;
using HeritageAcademy.Models.DataModels;

namespace HeritageAcademy.Models.ViewModels
{
    public class CourseLocationToPositionConverter : IMultiValueConverter
    {
        #region IMultiValueConverter Members
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            // assume the first object is a BookViewModel and the second object is a BookLocation
            var viewModel = values[0] as CourseViewModel;
            var location = values[1] as CourseLocation;

            // try to reverse theme
            if (viewModel == null && location == null)
            {
                viewModel = values[1] as CourseViewModel;
                location = values[0] as CourseLocation;
            }

            // if still null, we give up
            if (viewModel == null || location == null) return null;

            // otherwise, we return and int that represents the current position
            if (viewModel.Paginator != null)
            {
                return viewModel.Paginator.BookLocationToPosition(location);
            }

            return null;
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            // not implemeneted
            return null;
        }
        #endregion
    }
}